package com.guigu.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.guigu.health.pojo.Order;
import com.guigu.health.pojo.OrderCheckItemDetails;
import com.guigu.health.pojo.OrderExample;
import com.guigu.health.pojo.OrderSetMemalDetails;
import com.guigu.health.service.OrderDetails;
import com.guigu.health.service.OrderService;
import com.mysql.jdbc.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName:OrderController
 * @Description:预约控制层
 * @Author:longtao
 * @Date:2021/4/11
 * @Version:1.0
 */

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {
    @Reference
    private OrderDetails orderDetails;

    @Reference
    private OrderService orderService;
    /**
     * 查询预约套餐详细数据
     *
     * @param name
     * @param pageNum
     * @return
     */
    @RequiresPermissions(value = {"ORDER_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/querySetMemal", method = RequestMethod.GET, produces = {"application/json;charset=utf-8"})
    public Object querySetMemal(String name, String pageNum) {
        Integer pageNum2 = null;
        if (StringUtils.isNullOrEmpty(pageNum)) {
            pageNum = "1";
        }
        if (StringUtils.isNullOrEmpty(name)) {
            name = "";
        }
        pageNum2 = Integer.valueOf(pageNum);
        PageInfo<OrderSetMemalDetails> pageInfo = orderDetails.queryAllSetMemal(name, pageNum2, 2);
        return pageInfo;
    }

    /**
     * 查询预约检查项详细表
     *
     * @param name
     * @param pageNum
     * @return
     */
    @RequiresPermissions(value = {"ORDER_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/queryCheckItem", method = RequestMethod.GET, produces = {"application/json;charset=utf-8"})
    public Object queryCheckItem(String name, String pageNum) {
        Integer pageNum2 = null;
        if (StringUtils.isNullOrEmpty(pageNum)) {
            pageNum = "1";
        }
        if (StringUtils.isNullOrEmpty(name)) {
            name = "";
        }
        pageNum2 = Integer.valueOf(pageNum);
        PageInfo<OrderCheckItemDetails> pageInfo = orderDetails.queryAllCheckItem(name, pageNum2, 2);
        return pageInfo;
    }

    /**
     * 删除预约详细
     *
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"ORDER_DELETE"})
    @ResponseBody
    @RequestMapping(value = "/deleteOrder", method = RequestMethod.GET, produces = {"application/json;charset=utf-8"})
    public Object deleteOrder(String id) {
        return orderDetails.deleteDetails(Integer.valueOf(id));
    }

    /**
     * 修改预约详细
     *
     * @param order
     * @return
     */
    @RequiresPermissions(value = {"ORDER_UPDATE"})
    @ResponseBody
    @RequestMapping(value = "/updateOrder", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    public Object updateOrder(@RequestBody Order order) {
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(order.getId());
        boolean b = orderDetails.updateOrder(order, example);
        return b;
    }

    /**
     * 显示预约曲线图
     * @return
     */
    @RequiresPermissions(value = {"ORDERSETTING"})
    @ResponseBody
    @RequestMapping(value = "/getMemberReport",method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public Object getMemberReport(String length) {

        if (StringUtils.isNullOrEmpty(length)){
            length="7";
        }
        if (Integer.valueOf(length)>=15){
            length="15";
        }
        if (Integer.valueOf(length)<=7){
            length="7";
        }
        List<String> months=new ArrayList<String>();
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DATE,-Integer.valueOf(length));
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i <Integer.valueOf(length) ; i++) {
            calendar.add(Calendar.DATE,+1);
            Date date=calendar.getTime();
            months.add(sdf.format(date));
        }
        List<Long> orderCount=orderService.getCount(months);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("months",months);
        map.put("orderCount",orderCount);
        return map;
    }
}
